<template>
  <div id="addPictureBatchPage">
    <!-- 页面复用 -->
    <h2 style="margin-bottom: 20px; font-weight: bold">批量创建图片</h2>
    <!-- 图片信息表单 -->
    <a-form name="formData" layout="vertical" :model="formData" @finish="handleSubmit">
      <a-form-item name="searchText" label="关键词">
        <a-input v-model:value="formData.searchText" placeholder="请输入关键词" allow-clear />
      </a-form-item>
      <a-form-item name="count" label="抓取数量">
        <a-input-number
          v-model:value="formData.count"
          placeholder="请输入抓取数量"
          style="min-width: 180px"
          min="1"
          max="30"
          allow-clear
        />
      </a-form-item>

      <a-form-item name="namePrefix" label="名称前缀">
        <a-auto-complete
          v-model:value="formData.namePrefix"
          placeholder="请输入名称前缀，自动补充序号"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading">执行 Execute</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { uploadPictureByBatchUsingPost } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

// 图片VO
const picture = ref<API.PictureVO>()
const formData = reactive<API.PictureUploadByBatchRequest>({
  count: 10,
})

const router = useRouter()
// 提交任务状态
const loading = ref(false)
/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  loading.value = true
  const res = await uploadPictureByBatchUsingPost({
    ...formData,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success(`抓取成功，共${res.data.data}条数据！`)
    // 跳转到主页
    router.push({
      path: '/',
    })
  } else {
    message.error('创建失败，' + res.data.message)
  }
  loading.value = false
}
</script>

<style scoped>
#addPictureBatchPage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
